﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using PowerManager.Framework.DataPresentation.ExchangeableTarget;
using PowerManager.Framework.DataPresentation.UpdateStrategy;
using System.Net;
using PowerManager.Framework.DataPresentation;
using PowerManager.Framework.ModleBuilder;

namespace PowerManager.Framework.DataTransfer
{
    internal sealed class TransferTask
    {
        private IUpdateStrategy _updateStrategy;
        private IPAddress _ipAddress;

        #region Properties

        public PropertyInfo Property { get; set; }
        public IExchangeableTarget ExchangeableTarget { get; set; }
        public IUpdateStrategy UpdateStrategy {
            get { return _updateStrategy ?? (_updateStrategy = new OneTimeUpdate()); }
            set { _updateStrategy = value; }
        }
        public IPAddress IpAddress {
            get { return _ipAddress ?? (_ipAddress = IPAddress.None); }
            set { _ipAddress = value; }
        }
        public IModel Model { get; set; }
        #endregion

        internal void Execute() {
            initial();
            _updateStrategy.ExecuteStrategy();
        }

        private void initial() {
            UpdateStrategy.UpdateAction += TransfingData;
        }

        private void TransfingData(Object sender, EventArgs e) {
            if (ExchangeableTarget.DataType == ObjectDataType.Single) {
                Property.SetValue(Model, SnmpDataHelper.GetSingle(ExchangeableTarget, _ipAddress), null);
                Model.OnPropertyChanged(Property.Name);
            } else if (ExchangeableTarget.DataType == ObjectDataType.Table) {
                Property.SetValue(Model, SnmpDataHelper.GetTable(ExchangeableTarget, _ipAddress), null);
                Model.OnPropertyChanged(Property.Name);
            }
        }
    }
}
